﻿Imports System.Data.OleDb
Partial Public Class Details
    Inherits System.Web.UI.Page
    Dim count As Integer = 0
    Dim petID As Int16
    Dim UserID As Int16
    Dim rate As Int16
    Dim showsticker As Boolean

    Dim CountLike As Int16 = 0
    Dim CountComment As Int16 = 0
    Dim TotalContribution As Int16 = 0



    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        '   Page_counter()
     

        If Request.QueryString("ID") = Nothing Then
        Else
            Dim ID As String = Request.QueryString("ID").ToString()

            Dim con As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings _
                                                 ("PetsConnectionString").ToString())

            con.Open()


            Dim sqlString As String = "select * from PetDetails where ID=" + ID + ""
            Dim cmd As OleDb.OleDbCommand = New OleDb.OleDbCommand(sqlString, con)
            Dim dr As OleDb.OleDbDataReader = cmd.ExecuteReader()
            While dr.Read()
                Me.lbl_Petname.Text = dr("PetName")

                Me.img_Pet.ImageUrl = dr("photo")
                Me.lbl_weight.Text = dr("Weight")
                Me.lbl_color.Text = dr("Colour")
                Me.lbl_age.Text = dr("Age")
                Me.lbl_advt.Text = dr("Advantage")
                Me.lbl_ownname.Text = dr("OwnerName")
                Me.lbl_des.Text = dr("Description")


            End While

            dr.Close()
            con.Close()

        End If
        load_lileofuser()

        Me.link_lbl.NavigateUrl = "Discussions.aspx?ID=" & Request.QueryString("ID")
        show_values_in_Sticker()
        seeing_the_sticker()

    End Sub

    Sub Page_counter()

        'Insert the data in database Table ##CountTB## to count that how many times THIS page has opened
        Dim con As New OleDbConnection(ConfigurationManager.ConnectionStrings("PetsConnectionString").ToString())
        Dim QRY As String = "INSERT INTO CounterCount (Time_open) values (@Time_open)"

        con.Open()
        Dim cmd As New OleDbCommand(QRY, con)

        cmd.Parameters.AddWithValue("Time_open", Date.Now.ToShortTimeString())

        cmd.ExecuteNonQuery()


        'Show that how many times it seen
        Dim SltQRY As String = "Select ID from CounterCount"
        Dim cmd1 As New OleDbCommand(SltQRY, con)

        Dim dr As OleDbDataReader = cmd1.ExecuteReader()
        If dr.HasRows Then
            While dr.Read
                count += 1
            End While
        End If

        Me.countshow_lbl.Text = "Page has been viewed " & count & " times"
        con.Close()

    End Sub

    Function findUSerID()
        Dim con As New OleDbConnection(ConfigurationManager.ConnectionStrings("PetsConnectionString").ToString())
        con.Open()

        Dim oledbcmd As New OleDbCommand("SELECT * From aspnet_Users where UserName = @Username", con)
        oledbcmd.Parameters.AddWithValue("@Username", User.Identity.Name)
        Dim dr As OleDbDataReader = oledbcmd.ExecuteReader()
        dr.Read()
        UserID = dr("UserId")
        Return UserID
    End Function

    Protected Sub comment_btn_Click(ByVal sender As Object, ByVal e As EventArgs) Handles comment_btn.Click
        petID = Request.QueryString("ID")
        Dim con As New OleDbConnection(ConfigurationManager.ConnectionStrings("PetsConnectionString").ToString())
        Dim QRY As String = "INSERT INTO Comments (Comment,UserName,PetID,Time_comment,Photo,UserID) values (@Comment,UserName,PetID,Time_comment,Photo,UserID)"

        con.Open()
        Dim cmd As New OleDbCommand(QRY, con)
        cmd.Parameters.AddWithValue("Comment", Me.TB_COMMENTS.Text)
        cmd.Parameters.AddWithValue("UserName", User.Identity.Name)
        cmd.Parameters.AddWithValue("PetID", petID)
        cmd.Parameters.AddWithValue("Time_Comment", Date.Now.ToShortTimeString())
        cmd.Parameters.AddWithValue("Photo", userphoto())
        cmd.Parameters.AddWithValue("userID", findUSerID())
        cmd.ExecuteNonQuery()


        Me.TB_COMMENTS.Text = ""
        RefreshPage()
    End Sub
    Sub RefreshPage()
        If Request.QueryString("ID") = Nothing Then
        Else
            Response.Redirect("Details.aspx?ID=" & Request.QueryString("ID"))
        End If

    End Sub
    Function userphoto()
        Dim uname As String
        Dim con As New OleDbConnection(ConfigurationManager.ConnectionStrings("PetsConnectionString").ToString())
        con.Open()
        Dim oledbcmd As New OleDbCommand("SELECT * From UserDetails where UserName = '" + User.Identity.Name + "'", con)
        oledbcmd.Parameters.AddWithValue("@Username", User.Identity.Name)

        Dim dr As OleDbDataReader = oledbcmd.ExecuteReader()
        dr.Read()
        uname = dr("Photo").ToString()

        Return uname
        con.Close()
        dr.Close()


    End Function

    Private Sub Rating1_Changed(ByVal sender As Object, ByVal e As AjaxControlToolkit.RatingEventArgs) Handles Rating1.Changed
        rate = e.Value
    End Sub

    Sub storeRating(ByVal userid As Int16, ByVal petid As Int16, ByVal rating As Int16)
        Dim con As New OleDbConnection(ConfigurationManager.ConnectionStrings("PetsConnectionString").ToString())
        Dim sqlqry As String = "INSERT INTO Ratings_tbl (Rating,User_ID,Pet_ID) values (@Rating,@User_ID,@Pet_ID)"
        con.Open()
        Dim cmd As New OleDbCommand(sqlqry, con)
        cmd.Parameters.AddWithValue("Rating", rating)
        cmd.Parameters.AddWithValue("User_ID", userid)
        cmd.Parameters.AddWithValue("Pet_ID", petid)

        cmd.ExecuteNonQuery()
        con.Close()



    End Sub

    Protected Sub Submit_btn_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Submit_btn.Click
        storeRating(findUSerID(), Request.QueryString("ID"), Me.Rating1.CurrentRating)

        RefreshPage()
    End Sub

    Protected Sub btn_submitlikes_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btn_submitlikes.Click
        Dim conDEL As New OleDbConnection(ConfigurationManager.ConnectionStrings("PetsConnectionString").ToString())
        conDEL.Open()
        Dim sqlqryDEL As String = "DELETE * from Likes_tbl where user_ID= " & findUSerID() & " AND Pet_ID= " & Request.QueryString("ID") & ""
        Dim cmdDEL As New OleDbCommand(sqlqryDEL, conDEL)
        cmdDEL.ExecuteNonQuery()
        conDEL.Close()


        Dim con As New OleDbConnection(ConfigurationManager.ConnectionStrings("PetsConnectionString").ToString())
        con.Open()

        Dim sqlqry As String = "INSERT INTO Likes_tbl (Like_value,User_ID,Pet_ID) values (@Like_value,@User_ID,@Pet_ID)"

        Dim cmd As New OleDbCommand(sqlqry, con)
        If (CheckBox1.Checked) Then
            cmd.Parameters.AddWithValue("Like_value", "Like")
        Else
            cmd.Parameters.AddWithValue("Like_value", "Dont Like")

        End If

        cmd.Parameters.AddWithValue("User_ID", findUSerID())
        cmd.Parameters.AddWithValue("Pet_ID", Request.QueryString("ID"))
        cmd.ExecuteNonQuery()

        RefreshPage()
    End Sub

    

    Sub load_lileofuser()
        Dim countPeople As Integer
        Dim lbltext As String = " People Like This"
        Dim con As New OleDbConnection(ConfigurationManager.ConnectionStrings("PetsConnectionString").ToString())
        Try
            Dim SltQRY As String = "Select * from Likes_tbl where pet_ID = " & Request.QueryString("ID").ToString() & ""
            con.Open()

            Dim cmd As New OleDbCommand(SltQRY, con)

            Dim dr As OleDbDataReader = cmd.ExecuteReader()
            If dr.HasRows Then
                While dr.Read
                    If dr("Like_value") = "Like" Then
                        countPeople += 1
                        lbltext = " People Like This"
                    Else

                    End If

                End While
            End If
            Me.lbl_showlike.Text = countPeople & lbltext
        Catch ex As Exception

        End Try

    End Sub

    Protected Sub placesticker_btn_Click(ByVal sender As Object, ByVal e As EventArgs) Handles placesticker_btn.Click
        seeing_the_sticker()

    End Sub

    Protected Sub CheckBox1_CheckedChanged(ByVal sender As Object, ByVal e As EventArgs) Handles CheckBox1.CheckedChanged
        'If CheckBox1.Checked = True Then
        '    MsgBox("true")
        'Else
        '    MsgBox("False")
        'End If
    End Sub

    Function Check_people_Contribution() As Boolean
        ' Count the Total Likes/Dislikes by user
        Dim con As New OleDbConnection(ConfigurationManager.ConnectionStrings("PetsConnectionString").ToString())
        Dim SltQRY As String = "Select * from Likes_tbl where user_ID = " & findUSerID() & ""
        con.Open()

        Dim cmd As New OleDbCommand(SltQRY, con)

        Dim dr As OleDbDataReader = cmd.ExecuteReader()
        If dr.HasRows Then
            While dr.Read

                CountLike += 1
                
            End While
        End If
        con.Close()


        ' count total comment by the user
        Dim SltQRY1 As String = "Select * from Comments where UserName = '" + User.Identity.Name + "'"
        con.Open()

        Dim cmd1 As New OleDbCommand(SltQRY1, con)

        Dim dr1 As OleDbDataReader = cmd1.ExecuteReader()
        If dr1.HasRows Then
            While dr1.Read

                CountComment += 1
               
            End While
        End If
        TotalContribution = CountComment + CountLike
        If TotalContribution > 3 Then
            Return True
        Else
            Return False
        End If

    End Function

    Sub seeing_the_sticker()
        Try
            Dim con As New OleDbConnection(ConfigurationManager.ConnectionStrings("PetsConnectionString").ToString())
            con.Open()
            Dim sqlString As String = "select * from PetDetails where ID=" + Request.QueryString("ID").ToString() + _
            " AND OwnerName = " & findUSerID() & ""

            Dim cmd As OleDb.OleDbCommand = New OleDb.OleDbCommand(sqlString, con)
            Dim dr As OleDb.OleDbDataReader = cmd.ExecuteReader()
            dr.Read()
            If dr.HasRows Then
                'Yes the pet is yours (Loged in user's)
                If Check_people_Contribution() = True Then
                    'Yes this is a Importaint User
                    Dim con1 As New OleDbConnection(ConfigurationManager.ConnectionStrings("PetsConnectionString").ToString())
                    con1.Open()
                    Dim sqlString1 As String = "select * from sticker_detail where petid = '" + Request.QueryString("ID") + "'"


                    Dim cmd1 As OleDbCommand = New OleDbCommand(sqlString1, con1)
                    Dim dr1 As OleDb.OleDbDataReader = cmd1.ExecuteReader()
                    dr1.Read()
                    If dr1.HasRows Then
                        ' Yes there is a data inu the Table
                        showsticker = True
                        'Show the data
                        Me.TB_sticker.Text = dr1("Content")
                        'Show the panel
                        Me.container.Visible = True
                        Me.placesticker_btn.Enabled = True
                        'show delete button and edit button also
                        ' write code for this
                    Else
                        ' No there is no data in the table
                        ' ENable the showing burtton
                        '   Me.


                    End If
                    con.Close()



                    Me.container.Visible = True
                    Me.TB_sticker.ReadOnly = False
                Else
                    ' no you are not a imp user but pet is yours
                    Me.placesticker_btn.Enabled = False
                    '       Me.placesticker_btn.Text = "Sorry Still you cant able to place any sticker on the pet"
                    Me.errorlbl.Text = "can't place the sticker because Still you are not a importaint member of the pets care"
                End If

            Else
                ' its not your pet
                Me.errorlbl.Text = "Sorry you cant place the sticker on this pet because it not yours"
                '    If showsticker = True Then ' MEans there i 
                ' There is  a data in table then show the sticker
                Dim con2 As New OleDbConnection(ConfigurationManager.ConnectionStrings("PetsConnectionString").ToString())
                con2.Open()
                Dim sqlString2 As String = "select * from sticker_detail where petid = '" + Request.QueryString("ID") + "'"


                Dim cmd2 As OleDbCommand = New OleDbCommand(sqlString2, con2)
                Dim dr2 As OleDb.OleDbDataReader = cmd2.ExecuteReader()
                dr2.Read()
                If dr2.HasRows Then

                    Me.TB_sticker.Text = dr2("Content")
                    Me.TB_sticker.ReadOnly = True
                    Me.container.Visible = True
                    Me.btn_StikySubmt.Visible = False
                    Me.btn_StickyDelete.Visible = False
                    Me.placesticker_btn.Enabled = False
                Else
                    ' No there is no data in the table
                    Me.container.Visible = False
                End If

            End If

        Catch ex As Exception
            MsgBox("You are going or do something wrong !!! " & Environment.NewLine & "we will redirect you to Home page..", MsgBoxStyle.Critical, "Error!!!!")
            Response.Redirect("Home.aspx")

        End Try
       

        'dr.Close()
        'con.Close()
    End Sub

    Sub show_values_in_Sticker()
        Dim con As New OleDbConnection(ConfigurationManager.ConnectionStrings("PetsConnectionString").ToString())
        con.Open()
        Dim sqlString As String = "select * from sticker_detail where petid = '" + Request.QueryString("ID") + "'"


        Dim cmd As OleDbCommand = New OleDb.OleDbCommand(sqlString, con)
        Dim dr As OleDb.OleDbDataReader = cmd.ExecuteReader()
        dr.Read()
        If dr.HasRows Then
            showsticker = True
            If dr("userid") = findUSerID() Then
                Me.TB_sticker.Text = dr("Content")
                Me.placesticker_btn.Enabled = True
            End If
        Else
        End If

        con.Close()

    End Sub


    Protected Sub btn_StikySubmt_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btn_StikySubmt.Click
        Dim con1 As New OleDbConnection(ConfigurationManager.ConnectionStrings("PetsConnectionString").ToString())
        con1.Open()
        Dim sqlqry1 As String = "Delete * FROM sticker_detail where username = '" + User.Identity.Name + "' AND PetID = '" + Request.QueryString("ID") + "'"
        Dim cmd1 As New OleDbCommand(sqlqry1, con1)
        cmd1.ExecuteNonQuery()
        con1.Close()

        Dim con As New OleDbConnection(ConfigurationManager.ConnectionStrings("PetsConnectionString").ToString())
        con.Open()
        Dim sqlqry As String = "INSERT INTO sticker_detail(Content,userid,username,petid,ShownTo) values (@Content,@userid,@username,@petid,@ShownTo)"
        Dim cmd As New OleDbCommand(sqlqry, con)
        cmd.Parameters.AddWithValue("@Content", Me.TB_sticker.Text)
        cmd.Parameters.AddWithValue("@userid", findUSerID())
        cmd.Parameters.AddWithValue("@username", User.Identity.Name)
        cmd.Parameters.AddWithValue("@petid", Request.QueryString("ID"))
        cmd.Parameters.AddWithValue("@ShownTo", "Yes")
        cmd.ExecuteNonQuery()
    End Sub

    Protected Sub btn_submittag_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btn_submittag.Click
        Dim con1 As New OleDbConnection(ConfigurationManager.ConnectionStrings("PetsConnectionString").ToString())
        con1.Open()

        For Each NewItem As ListItem In tags_list_checkBoxes.Items
            If NewItem.Selected Then


                Dim sqlqry As String = "INSERT INTO TaggingJunction_tbl(petID,UserID,TagID) values (@petID,@UserID,@TagID)"

                Dim cmd1 As New OleDbCommand(sqlqry, con1)
                cmd1.Parameters.AddWithValue("@perID", Request.QueryString("ID"))
                cmd1.Parameters.AddWithValue("@UserID", findUSerID())
                cmd1.Parameters.AddWithValue("@TagID", NewItem.Value)
                cmd1.ExecuteNonQuery()

                
            End If

        Next
        RefreshPage()
    End Sub

    Protected Sub btn_SubmitNewTAG_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btn_SubmitNewTAG.Click
        Dim con1 As New OleDbConnection(ConfigurationManager.ConnectionStrings("PetsConnectionString").ToString())
        con1.Open()



        Dim sqlqry As String = "INSERT INTO Tags_tbl(TagName) values (@TagName)"

        Dim cmd1 As New OleDbCommand(sqlqry, con1)
        'cmd1.Parameters.AddWithValue("@perID", Request.QueryString("ID"))
        cmd1.Parameters.AddWithValue("@TagName", Me.tb_newtag.Text)

        cmd1.ExecuteNonQuery()

        Response.Redirect("Details.aspx?ID=" & Request.QueryString("ID"))
        RefreshPage()
    End Sub

    Protected Sub btn_StickyDelete_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btn_StickyDelete.Click
        Dim con As New OleDbConnection(ConfigurationManager.ConnectionStrings("PetsConnectionString").ToString())
        con.Open()
        Dim sqlqry As String = "Delete * FROM sticker_detail where username = '" + User.Identity.Name + "' AND PetID = '" + Request.QueryString("ID") + "'"
        Dim cmd As New OleDbCommand(sqlqry, con)
        cmd.ExecuteNonQuery()
        con.Close()

        Response.Redirect("Details.aspx?ID=" & Request.QueryString("ID"))


    End Sub
End Class